package controller;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import sql.BookLookUp;
import bean.BookBean;

public class BookInformation extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	{
		//For Testing
		//ISBN: 0130473820
		//Author: John Dewey
		//Title: The Law
		//Keyword: art
		
		HttpSession session = request.getSession(true);
		session.setAttribute("bookList", null);
		String isbn = request.getParameter("isbn");
		String authorFirstName = request.getParameter("authorFirstName");
		String authorLastName = request.getParameter("authorLastName");
		String title = request.getParameter("title");
		String keyword = request.getParameter("keyword");
		
		BookLookUp bookLookUp = new BookLookUp();		
		try 
		{
			HashMap<Integer, BookBean> bookList = new HashMap<Integer, BookBean>();
			bookList = bookLookUp.getBookInfo(isbn, authorFirstName, authorLastName, title, keyword);
			String address = "/WEB-INF/bookInformationDisplay.jsp";
			RequestDispatcher dispatcher = request.getRequestDispatcher(address);			
						
			if(bookList != null && bookList.size()!= 0)
			{
				session.setAttribute("bookList", bookList);				
			}
			else
			{
				session.removeAttribute("book");
			}
			
			//redirect page based on user login
			if(session.getAttribute("customer") !=null)
			{
				dispatcher.forward(request, response);
			}
			else
			{
				response.sendRedirect("../bookInformationDisplay.jsp");
			}
			
		} catch (ClassNotFoundException e) {e.printStackTrace();
		} catch (SQLException e) { e.printStackTrace(); 
		} catch (IOException e) {e.printStackTrace();
		} catch (ServletException e) { e.printStackTrace();
		}
			
	}
}
